//
//  ConvolveFilterViewController.m
//  vImageStudy
//
//  Created by Evan Xie on 8/7/15.
//  Copyright (c) 2015 Family Wings. All rights reserved.
//

#import "ConvolveFilterViewController.h"
#import "vImageProcessor.h"
#import "CIFilterProcessor.h"
#import "RadioButton.h"
#import "RadioButtonGroup.h"


typedef NS_ENUM(NSUInteger, FilterIndex) {
    FilterIndexOriginal = 0,
    FilterIndexEmboss,
    FilterIndexGausianBlur
};


typedef NS_ENUM(NSUInteger, FilterAlgorithm) {
    FilterAlgorithmVImage = 0,
    FilterAlgorithmCIFilter
};


@interface ConvolveFilterViewController () <UIPickerViewDelegate, UIPickerViewDataSource, RadioButtonGroupDelegate>
{
    UIImage *_originalImage;
}

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIPickerView *filterPickerView;

@property (nonatomic, strong) NSArray *filterNames;
@property (strong, nonatomic) IBOutletCollection(RadioButton) NSArray *radioButtons;

@property (nonatomic, strong) RadioButtonGroup *buttonGroup;

@end

@implementation ConvolveFilterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _filterNames = @[@"Original", @"Emboss", @"Gaussian Blur"];
    
    _originalImage = [UIImage imageNamed:@"skd188779sdc.png"];
    
    _buttonGroup = [[RadioButtonGroup alloc] initWithRadioButtons:self.radioButtons];
    _buttonGroup.selectedIndex = FilterAlgorithmVImage;
    _buttonGroup.selectedIndex = 0;
    _buttonGroup.delegate = self;
    
    self.imageView.image = _originalImage;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)addBarButtonItemPressed:(id)sender
{
   
}


#pragma mark - Filter Operations

- (void)doEmbossEffect
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        UIImage *outputImage = nil;
        
        if (_buttonGroup.selectedIndex == FilterAlgorithmVImage) {
            
            outputImage = [vImageProcessor embossEffectForImage:_originalImage];
            
        } else if (_buttonGroup.selectedIndex == FilterAlgorithmCIFilter) {
            
            outputImage = [CIFilterProcessor embossEffectForImage:_originalImage];
            
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            self.imageView.image = outputImage;
        });
    });
}

- (void)doGaussianBlurEffect
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        UIImage *outputImage = nil;
        
        if (_buttonGroup.selectedIndex == FilterAlgorithmVImage) {
            
            outputImage = [vImageProcessor gaussianBlurEffectForImage:_originalImage blurRadius:50.0];
            
        } else if (_buttonGroup.selectedIndex == FilterAlgorithmCIFilter) {
            
            outputImage = [CIFilterProcessor gaussianBlurEffectForImage:_originalImage];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
           self.imageView.image = outputImage;
        });
    });
}


#pragma mark - RadioButtonGroupDelegage

- (void)radioButtonGroup:(RadioButtonGroup *)group didSelectAtIndex:(NSInteger)buttonIndex
{
    NSInteger selectedRow = [self.filterPickerView selectedRowInComponent:0];
    [self pickerView:self.filterPickerView didSelectRow:selectedRow inComponent:0];
}

#pragma mark - UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.filterNames.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return self.filterNames[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    switch (row) {
        case FilterIndexOriginal:
            self.imageView.image = _originalImage;
            break;
            
        case FilterIndexEmboss:
            [self doEmbossEffect];
            break;
            
        case FilterIndexGausianBlur:
            [self doGaussianBlurEffect];
            break;
            
        default:
            [[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Not implemented yet" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil] show];
            break;
    }
}

@end
